﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Drawing;
using Intorise.Common;
using System.Text.RegularExpressions;

namespace Model
{
    public partial class Player
    {
        /// <summary>
        /// 正在执行特殊任务
        /// </summary>
        public bool taskRunning = false;
        /// <summary>
        /// 转生等级1-一转，2-二转
        /// </summary>
        public int ClassLevel = 0;

        #region 获取等级相关

        Regex levelRegex = new Regex("\\(([0-9]+)级");

        /// <summary>
        /// 组队
        /// </summary>
        public void Zuidui()
        {
            //00 00 00 04 00 01 CD B9 
            engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0xCD, 0xB9 });
        }

        /// <summary>
        /// 退组
        /// </summary>
        public void Tuizu()
        {
            //00 00 00 0E 00 01 CD BE D4 E4 C8 9F E4 EA AB 99 75 01 
            var datas = Tools.Contact(new byte[] { 0x00, 0x00, 0x00, 0x0E, 0x00, 0x01, 0xCD, 0xBE },
                DataPackager.GetNetworkData(this.ID), this.Version, new byte[] { 0x01 });
            engine.SendData(datas);
        }

        public bool ProcessGetLevelMsg(Message x)
        {
            GameHelper.Run(x, r =>
            {
                var text = Tools.GetString(x.Data);
                if (LogDebugInfo)
                {
                    ConsoleBLL.WriteLine(text);
                }
                if (text.Contains(this.Name) && text.Contains("离开了队伍"))
                {
                    var match = levelRegex.Match(text);
                    if (match.Success)
                    {
                        this.Level = int.Parse(match.Groups[1].Value);
                        ConsoleBLL.WriteLineInfo(this + " 当前等级：" + this.Level);
                    }
                }
            });
            return true;
        }
        #endregion

        #region 人物死亡

        public bool ProcessKilledMsg(Message x)
        {
            GameHelper.Run(x, r =>
            {
                var targetID = DataPackager.GetHostData64(x.Data, 0);
                var attackerID = DataPackager.GetHostData64(x.Data, 14);
                var length = DataPackager.GetHostData16(x.Data, 23);
                var name = Tools.GetString(x.Data, 25, length);
                if (targetID == this.ID)
                {
                    CurrentHP = 0;
                    ConsoleBLL.WriteLineError(this + " 被【" + name + "】" + attackerID.ToString("X2") + " 杀死");
                    GameHelper.RunGameThread(ReFight);
                }
                else
                {
                    ConsoleBLL.WriteLineError(targetID.ToString("X2") + " 被【" + name + "】" + attackerID.ToString("X2") + " 杀死");
                }
            });
            return true;
        }
        #endregion

        private void Use称号(long itemID)
        {
            //0000   00 00 00 0d 00 08 13 37 84 f0 a7 b5 bb eb ab 99 75
            //00000007000803ddca9c01
            //engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x07, 0x00, 0x08, 0x03, 0xdd, 0xca, 0x9c, 0x01 });
            //Thread.Sleep(100);
            engine.SendData(Tools.Contact(new byte[] { 00, 0x00, 0x00, 0x0d, 0x00, 0x08, 0x13, 0x37 }, DataPackager.GetNetworkData(itemID), this.Version));
            Thread.Sleep(100);
            //engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x07, 0x00, 0x08, 0x13, 0x38, 0x92, 0xd8, 0x03 });
        }

        /// <summary>
        /// 佩戴称号
        /// </summary>
        /// <param name="title">称号数据</param>
        /// <returns></returns>
        public bool PdItem(byte[] title)//佩戴称号
        {
            var data = Tools.Contact(new byte[] { 0x00, 0x00, 0x00, 0x07, 0x00, 0x08, 0x13, 0x38 }, title);
            engine.SendData(data);
            return true;
        }

        /// <summary>
        /// 整理背包
        /// </summary>
        public void CleanPackage()
        {
            engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x97, 0x0D }); //整理背包
        }

        /// <summary>
        /// 背包扩容
        /// </summary>
        /// <param name="count"></param>
        public void Action背包扩容(int count)
        {
            engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x05, 0x00, 0x01, 0x97, 0x0F, (byte)(count * 2) });
        }

        /// <summary>
        /// 升级坐骑
        /// </summary>
        /// <param name="count"></param>
        public void UpgradeHorse(int count = 1)
        {
            if (LogDebugInfo)
            {
                ConsoleBLL.WriteLineInfo(this + "开始升级坐骑");
            }
            for (var i = 0; i < count; i++)
            {
                engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x07, 0x00, 0x01, 0xEC, 0xFC, 0x00, 0x00, 0x00 });
                Thread.Sleep(100);
            }
        }

        /// <summary>
        /// 丢弃物品
        /// </summary>
        /// <param name="index">整理背包后的物品索引从1开始</param>
        public void DestoryItem(int index)
        {
            if (LogDebugInfo)
            {
                ConsoleBLL.WriteLineInfo(this + "丢弃物品");
            }
            engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x05, 0x00, 0x01, 0x97, 0x11, (byte)(index * 2) });
            Thread.Sleep(100);
        }

        public void Add力量(int count = 1)
        {
            engine.SendData(DataPackager.GetAdd力量Data(count));
        }
        public void Add敏捷(int count = 1)
        {
            engine.SendData(DataPackager.GetAdd敏捷Data(count));
        }
        public void Add体力(int count = 1)
        {
            engine.SendData(DataPackager.GetAdd体力Data(count));
        }
        public void Add智力(int count = 1)
        {
            engine.SendData(DataPackager.GetAdd智力Data(count));
        }

        /// <summary>
        /// 分配剩余点数
        /// </summary>
        /// <param name="count"></param>
        public void DispatchPoint(int count)
        {
            if (count > 5)
            {
                if (Profession == ProfessionEnum.法师)
                {
                    Add力量(count / 5); Add智力(count * 3 / 5); Add敏捷(count / 5);
                }
                else if (Profession == ProfessionEnum.弓手)
                {
                    Add力量(count / 5); Add敏捷(count * 4 / 5);
                }
                if (LogDebugInfo)
                {
                    ConsoleBLL.WriteLineInfo(this + "分配点数完毕");
                }
            }
        }

        public void Buy黑龙波()
        {
            ConsoleBLL.WriteLineInfo(this + " 购买黑龙波...");
            engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x18, 0x00, 0x01, 0x9A, 0xF1, 0xE4, 0xD4, 0x03, 0xBA, 0x03, 0x02, 0x08, 0xB2, 0xCA, 0x55, 0x00, 0x00, 0xD0, 0x07, 0x02, 0x00, 0xD0, 0x07, 0x02, 0x02 });
        }

        public void Add怒意加深(int count = 4)
        {
            for (var i = 0; i < count; i++)
            {
                engine.SendData(new byte[] { 0x00, 0x00, 0x00, 0x05, 0x00, 0x09, 0x28, 0x8E, 0x02 });//激活怒意加深
            }
        }

        public void Action分解图鉴(Thing item)
        {
            if (LogDebugInfo)
            {
                ConsoleBLL.WriteLine(this + "分解图鉴：" + item.Name);
            }
            engine.SendData(DataPackager.GetAction分解图鉴Data(item));
        }

        public void Action装备图鉴(Thing item)
        {

        }

        public void Action强化装备(Thing item)
        {
            if (LogDebugInfo)
            {
                ConsoleBLL.WriteLine(this + "强化装备：" + item.Name);
            }
            engine.SendData(DataPackager.GetAction强化Data(item));
        }
    }
}
